<?php 
	namespace Home\Controller;
	use Think\Controller;

	class ProgramInfoeController extends Controller
	{
		public function index(){return;}
		/**
		 * 满足条件剧集
		 * @param  [type] $userid [description]
		 * @return [type]         [description]
		 */
		public function get_series()
		{
			$userid = 24;
			$limit = 1;
			$token = "12kkddk";
			$return = ['code'=>404,'result'=>[]];
			// 获取省份id及类型
			$province_id = M('UserProvince')->where(['user_id'=>$userid,'province_id'=>['gt',1]])->field('province_id')->select();
			if(!$province_id){
				return $return;
			}
			$province_id = implode(',',array_column($province_id, 'province_id'));
			$proid = M("TDownload")->field("movie_id")->where(["province_id"=>["in",$province_id]])->select();
			$proid = implode(array_column($proid,"movie_id"),",");
			$proid == ""?$proid = 0:$proid;
			$id = M('ProgramProvince')->field('program_id')->where(['province_id'=>['IN',$province_id]])->select();
			$id = implode(array_column($id, 'program_id'),',');
			$province_type = M('TProvince')->where(['id'=>['IN',$province_id]])->field('type')->select();
			$province_type = implode(',',array_column($province_type, 'type'));
			if(!$province_type){
				return $return;
			}
			// 根据省份获取节目
   			$sql = "SELECT a.id id, a.code code, a.cp_code cp_code, a.program_name program_name, a.parent_id parent_id, f.id movie_id, a.status status, a.episode episode, a.duration duration, f.code movie_code, f.type movie_type, f.file_url movie_file_url, f.source_DRM_type movie_source_DRM_type, f.dest_DRM_type movie_dest_DRM_type, f.audio_type movie_audio_type, f.screen_format movie_screen_format, f.closed_captioning movie_closed_captioning, f.video_bitrate movie_video_bitrate, f.create_at movie_create_at, f.update_at movie_update_at, f.program_code movie_program_code, f.program_id movie_program_id, f.parent_cp_code movie_parent_cp_code, f.cp_code movie_cp_code, f.cp_file_url movie_cp_file_url, f.command_id movie_command_id, f.status movie_status, f.download_status movie_download_status, f.is_cached movie_is_cached, f.source movie_source, f.rebuild movie_rebuild, f.filesize movie_file_size,f.filehash movie_file_hash, f.code_rate movie_code_rate FROM t_program AS a JOIN movie AS f ON f.program_id = a.id JOIN t_program AS g on a.parent_id = g.id WHERE a.parent_id IN ($id) AND a.status = 0 AND g.status = 0 AND f.status = 0 AND f.code_rate IN ({$province_type}) AND f.id NOT IN ($proid) order by f.id LIMIT ".$limit;
   			$series = M()->query($sql);	
   			if(empty($series)){
   				return $return;
   			}
   			$data = [];
   			$parent_id = [];
   			$movie_update = [];
   			$time = time();
   			$bit_name = C('CODERATE');
   			foreach ($series as $serie) {
   				$movie = [];
   				foreach ($serie as $key => $value) {
   					if(strpos($key,'movie_')===0){
   						$movie[str_replace('movie_', '', $key)] = $value;
   						unset($serie[$key]);
   					}
   				}
   				$movie['bit_name'] = isset($bit_name[$movie['code_rate']])?$bit_name[$movie['code_rate']]:'未知';
   				$data[] = [
   					'program_id' => $serie['parent_id'],
   					'serie' => $serie,
   					'movie' => $movie,
   				];
   				$parent_id[] = $serie['parent_id'];
   				$movie_update[] = [
   					'province_id' => $province_id,
   					'movie_id' => $movie['id'],
   					'create_time' => $time,
   					'update_time' => $time,
   					'download_status' => 0
   				];
   			}
   			// M('TDownload')->addAll($movie_update);
   			$parent_data = M('TProgram')->where(['id'=>['in',$parent_id]])->select(['index'=>'id']);
   			$picture_data = M('Picture')->alias('a')->where(['parent_id'=>['in',$parent_id]])->join('picture_info as b on a.picture_id = b.id')->select();
   			$program_picture = [];
   			foreach ($picture_data as $picture) {
   				if(!isset($program_picture[$picture['parent_id']])){
   					$program_picture[$picture['parent_id']] = [];
   				}
   				$program_picture[$picture['parent_id']][] = $picture;
   			}
   			foreach ($data as $key => $value) {
   				$data[$key]['program'] = $parent_data[$value['program_id']];
   				$data[$key]['program']['picture'] = $program_picture[$value['program_id']];
   			}
   			$return = ['code'=>200,'result'=>$data];
   			// 数据存在进行log操作
   			foreach ($data as $key => $value) {
   				$todata = [
   				   	"user_id" => $userid,
   					"movie_id" => $value['movie']['id'],
   					"province_id" => $province_id,
   					"action" => "dispatch",
   					"download_status" => 0,
   					"token" => $token,
   					"time" => time(),
   					"is_successed" => 1
   				];
   				D("Dispatch")->data($todata)->add();
   			}
   			return $return;
		}

		/**
		 * 公共接口
		 */
		public function all()
		{
			// 接收到参数
			$userid = I("get.userid","");
			$limit = I("get.limit","");
			$time = I("get.time","");
			$token = I("get.token","");
			$userid = intval($userid);
			$limit = intval($limit);
			$result = ['code'=>401,'result'=>[]];
			if($userid && $limit && $time && $token){
				// 来源认证
				$res = $this->check($userid,$time,$token);
				if($res){
					$result = $this->get_series($userid,$limit,$token);
				}
			}
			echo json_encode($result);
		}

		/**
		 * 下载反馈接口
		 */
		public function feedback()
		{
			// $province_id = intval(I("get.province_id",""));
			$userid = I("get.userid","");
			$time = I("get.time","");
			$token = I("get.token","");
			$where["user_id"] = $userid;
			$where["province_id"] = array('neq',1);
			$province_info = D("UserProvince")->where($where)->find();
			$province_id = $province_info['province_id'];
			$program_id = intval(I("get.program_id",""));
			$download_status = intval(I("get.download_status",""));
			if($province_id && $program_id && $download_status && $userid){
				// $res = $this->check($userid,$time,$token);
				$res = true;
				if($res){
					$map['province_id'] = $province_id;
					$map['program_id'] = $program_id;
					$data['download_status'] = $download_status;
					$data['update_time'] = time();
					$res_update = D("TDownload")->where($map)->save($data);
					// 数据存在进行log操作
		   			foreach ($data as $key => $value) {
		   				$todata2 = [
		   				   	"user_id" => $userid,
		   					"movie_id" => $program_id,
		   					"province_id" => $province_id,
		   					"action" => "feedback",
		   					"download_status" => $download_status,
		   					"token" => $token,
		   					"time" => time(),
		   					"is_successed" => 1
		   				];
		   				D("Dispatch")->data($todata2)->add();
		   			}
					if($res_update){
						$output = [
							"code"=>'200',
							"message"=>"更新成功"
						];
					}else{
						$output[] = [
							"code"=>'201',
							"message"=>"更新失败"
						];
					}
				}else{
						$output[] = [
							"code"=>'401',
							"message"=>"来源认证失败"
						];
				}
			}else{
				$output[] = [
					"code"=>'404',
					"message"=>"参数错误"
				];
			}
				echo json_encode($output);
		}

		/**
		 * 来源认证
		 * @return [type] [description]
		 */
		public function check($userid,$time,$token)
		{
			S("userid_".$userid)?S("userid_".$userid):S("userid_".$userid,0);   //时间缓存
			S("userid_".$useridToken,md5($userid.$time."ngOtUBy1ZKN1dwmm"));  //江西token缓存
			// dump(md5($userid.$time."ngOtUBy1ZKN1dwmm"));
			// dump($time);dump(S("userid_".$userid));dump($token);dump(S("userid_".$useridToken));exit();
			if(($time > S("userid_".$userid)) && ($token == S("userid_".$useridToken))){  
				S("userid_".$userid,$time);
				return true;
			}else{
				return false;
			}
		}

		public function md()
		{
			$time = time();
			$token = md5("24".$time."ngOtUBy1ZKN1dwmm");
			$url1 = "http://47.98.244.226:8090/ProgramInfo/all/userid/24/limit/1/time/".$time."/token/".$token;
			$url2 = "http://47.98.244.226:8090/ProgramInfo/feedback/program_id/117047/download_status/2/userid/24/time/".$time."/token/".$token;
			dump($url1);
			dump($url2);
		}
	}